package red.stu.mc.api;

import java.util.List;

import javax.annotation.Nullable;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraft.world.WorldType;

public final class Code {
	
	public IBlockState iBlockState;
	public red.stu.mc.api.clone.World worldIn;
	public BlockPos blockPos;
	public Block blockIn;
	public BlockPos fromBlockPos;
	
	public Code(IBlockState iBlockState, World worldIn, BlockPos blockPos, Block blockIn, BlockPos fromBlockPos) {
		this.iBlockState = iBlockState;
		if (worldIn != null)
			this.worldIn = new red.stu.mc.api.clone.World(worldIn);
		this.blockPos = blockPos;
		this.blockIn = blockIn;
		this.fromBlockPos = fromBlockPos;
	}
	
	public void output() {
		worldIn.instance.updateComparatorOutputLevel(blockPos, blockIn);
	}
	
	public boolean sleep(long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			// e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 获取当前Minecraft实例对象
	 * 		此对象执行的效果不会被保存，意味执行修改一旦遭到刷新就会被修复。
	 * 		如果您想实际修改，请直接使用'worldIn'变量
	 * @return Minecraft
	 */
	public Minecraft getMC() {
		return Minecraft.getMinecraft();
	}
	
	public World getNowWorld() {
		return getMC().world;
	}
	
	/**
	 * 给当前世界所有玩家发送消息 (缩写方法名)
	 * @param message 消息内容
	 */
	public void sendMsg(String message) {
		sendMessage(message);
	}
	
	/**
	 * 给当前世界所有玩家发送消息
	 * @param message
	 */
	public void sendMessage(String message) {
		if (message == null)
			message = "[null]";
		List<EntityPlayer> list =  worldIn.instance.playerEntities;
		if (list == null)
			return;
		ITextComponent component = new TextComponentString(message);
		for (EntityPlayer player : list) {
			player.sendMessage(component);
		}
	}
	
	/**
	 * 放置方块
	 * @param name 方块名
	 * @param x X坐标
	 * @param y Y坐标
	 * @param z Z坐标
	 * @return boolean 是否放置成功
	 */
	public boolean setBlock(String name, int x, int y, int z) {
		return setBlock(Block.getBlockFromName(name).getDefaultState(), x, y, z);
	}
	
	/**
	 * 放置方块
	 * @param state 方块状态 
	 * @param x X坐标
	 * @param y Y坐标
	 * @param z Z坐标
	 * @return boolean 是否放置成功
	 */
	public boolean setBlock(IBlockState state, int x, int y, int z) {
		return worldIn.instance.setBlockState(new BlockPos(x, y, z), state);
	}
	
	/**
	 * 放置空气方块
	 * @param x X坐标
	 * @param y Y坐标
	 * @param z Z坐标
	 * @return boolean 是否放置成功
	 */
	public boolean setBlockToAir(int x, int y, int z) {
		return worldIn.instance.setBlockToAir(new BlockPos(x, y, z));
	}
	
	/**
	 * 获取方块状态
	 * @param x X坐标
	 * @param y Y坐标
	 * @param z Z坐标
	 * @return IBlockState
	 */
	public IBlockState getBlockState(int x, int y, int z) {
		return worldIn.instance.getBlockState(new BlockPos(x, y, z));
	}
	
	/**
	 * 获取方块光不透明度
	 * @param x X坐标
	 * @param y Y坐标
	 * @param z Z坐标
	 * @return
	 */
	public int getBlockLightOpacity(int x, int y, int z) {
		return worldIn.instance.getBlockLightOpacity(new BlockPos(x, y, z));
	}
	
	/**
	 * 获取TileEntity
	 * @param x
	 * @param y
	 * @param z
	 * @return
	 */
	public TileEntity getTileEntity(double x, double y, double z) {
		return getTileEntity(new BlockPos(x, y, z));
	}
	
	/**
	 * 获取TileEntity
	 * @param pos
	 * @return
	 */
	public TileEntity getTileEntity(BlockPos pos) {
		return worldIn.instance.getTileEntity(pos);
	}
	
	/**
	 * 放置TileEntity
	 * @param x
	 * @param y
	 * @param z
	 * @param tileEntityIn
	 */
	public void setTileEntity(double x, double y, double z, @Nullable TileEntity tileEntityIn) {
		setTileEntity(new BlockPos(x, y, z), tileEntityIn);
	}
	
	/**
	 * 放置TileEntity
	 * @param pos 位置
	 * @param tileEntityIn TileEntity
	 */
	public void setTileEntity(BlockPos pos, @Nullable TileEntity tileEntityIn) {
		worldIn.instance.setTileEntity(pos, tileEntityIn);
	}
	
	/**
	 * 删除TileEntity
	 * @param x
	 * @param y
	 * @param z
	 */
	public void removeTileEntity(double x, double y, double z) {
		removeTileEntity(new BlockPos(x, y, z));
	}
	
	/**
	 * 删除TileEntity
	 * @param pos
	 */
	public void removeTileEntity(BlockPos pos) {
		worldIn.instance.removeTileEntity(pos);
	}
	
	/**
	 * 获取当前方块世界类型
	 * @return WorldType
	 */
	public WorldType getWorldType() {
		return worldIn.instance.getWorldType();
	}
	
	/**
	 * 获取当前方块世界类型ID
	 * @return int
	 */
	public int getWorldTypeId() {
		return getWorldType().getId();
	}
	
}
